package cn.anxingongyou.anxin.service.impl;

import cn.anxingongyou.anxin.entity.SignUp;
import cn.anxingongyou.anxin.entity.Work;
import cn.anxingongyou.anxin.mapper.WorkMapper;
import cn.anxingongyou.anxin.service.SignUpService;
import cn.anxingongyou.anxin.service.WorkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class WorkServiceImpl implements WorkService {

    private final WorkMapper workMapper;
    private final SignUpService signUpService;
    @Autowired
    public WorkServiceImpl(WorkMapper workMapper, SignUpService signUpService) {
        this.workMapper = workMapper;
        this.signUpService = signUpService;
    }

    @Override
    public int insert(Work work) {
        return workMapper.insertSelective(work);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return workMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Work work) {
        return workMapper.updateByPrimaryKeySelective(work);
    }

    @Override
    public Work selectOne(Work work) {
        return workMapper.selectOne(work);
    }

    @Override
    public Work selectOneByPrimaryKey(Long id) {
        return workMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Work> page(int pageNum, int pageSize, Work work) {
        String orderBy = "id desc";
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize,orderBy);
        List<Work> list = workMapper.select(work);
        PageInfo<Work> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Work> search(int pageNum, int pageSize, Work work) {
        String orderBy = "id desc";
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize,orderBy);

        Example example = new Example(Work.class);
        Example.Criteria criteria = example.createCriteria();
        if(work.getEmployeeName() != null){
            criteria.andLike("employeeName","%"+work.getEmployeeName()+"%");
        }
        if(work.getWorkName() != null){
            criteria.andLike("workName","%"+work.getWorkName()+"%");
        }
        if(work.getWorkType() != null){
            criteria.andLike("workType","%"+work.getWorkType()+"%");
        }
        if(work.getContacts() != null){
            criteria.andLike("contacts","%"+work.getContacts()+"%");
        }
        if(work.getPhone() != null){
            criteria.andEqualTo("phone",work.getPhone());
        }
        if(work.getId() != null){
            criteria.andEqualTo("id",work.getId());
        }
        if(work.getUserPid() != null){
            criteria.andEqualTo("userPid",work.getUserPid());
        }
        List<Work> list = workMapper.selectByExample(example);
        PageInfo<Work> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Work> range(int pageNum, int pageSize, BigDecimal rangeLong, BigDecimal rangeLati, Work work) {
        String orderBy = "id desc";
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize,orderBy);

        Example example = new Example(Work.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThan("workEndDatestamp",new Date());
        criteria.andGreaterThan("longitude",work.getLongitude().subtract(rangeLong));
        criteria.andGreaterThan("latitude",work.getLatitude().subtract(rangeLati));
        criteria.andLessThan("longitude",work.getLongitude().add(rangeLong));
        criteria.andLessThan("latitude",work.getLatitude().add(rangeLati));
        List<Work> list = workMapper.selectByExample(example);
        // 循环遍历结果，查找是否人数已满
        Iterator<Work> iterator = list.iterator();
        while(iterator.hasNext()){
            Work item = iterator.next();
            if(item.getWorkNumber() <= signUpService.countSignUpIsEmploy(item.getId(),1)){
                // 已经找满人了,移除该工作
                iterator.remove();
            }
        }
//        for (Work item : list){
//            if(item.getWorkNumber() <= signUpService.countSignUpIsEmploy(item.getId(),1)){
//                // 已经找满人了,移除该工作
//                list.remove(item);
//            }
//        }
        PageInfo<Work> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


}
